from xml.dom import minidom
import sys

class Persistense:
    _instance = None

    @staticmethod
    def instance():
        if (Persistense._instance is None):
            Persistense._instance = Persistense()

        return Persistense._instance

    _handler = None
    _series = {}

    _cache_timeout = None

    def get_header_file_path(self):
        return self.get_app_path() + 'img/header.gif'

    def get_icon_file_path(self):
        return self.get_app_path() + 'img/cc.ico'
    
    def get_data_file_path(self):
        return self.get_app_path() + 'data.xml'

    def get_app_path(self):
        return sys.path[0] + '/'


    def get_xml_tag_value(self, xmldoc, taglist, property_name):
        tag = taglist.pop(0)
        configs = xmldoc.getElementsByTagName(tag)
        if configs.length != 0:
            if (len(taglist) > 0):
                return self.get_xml_tag_value(configs.item(0),taglist, property_name)
            else:
                return self.get_xml_property_name(configs.item(0), property_name)
        else:
            return None

    def get_xml_property_name(self,tag, property_name):
        if tag.attributes.has_key(property_name):
            return tag.attributes[property_name].value
        return None

    def __init__(self):
        xmldoc = minidom.parse(self.get_data_file_path())

        cache_timeout = self.get_xml_tag_value(xmldoc.firstChild,['config','cache_timeout'],'value')

        if cache_timeout is None:
            self._cache_timeout = 60
        else:
            self._cache_timeout = int(cache_timeout)


        series = xmldoc.firstChild.getElementsByTagName('serie')

        for s in series:
            item = {}
            item['id'] = int(s.attributes["id"].value)
            item['season'] = int(s.attributes["season"].value)
            item['chapter'] = int(s.attributes["chapter"].value)
            self._series[str(s.attributes["id"].value)] = item

    def add_serie(self, idSerie, season, chapter):
        if (self._series.has_key(str(idSerie))):
            serie = self._series[str(idSerie)]
        else:
            serie = {}

        serie['id'] = idSerie
        serie['season'] = season
        serie['chapter'] = chapter

        self._series[str(idSerie)] = serie

    def get_series(self):
        return self._series

    def get_serie(self, idSerie):
        idSerie = str(idSerie)
        if (self.get_series().has_key(idSerie)):
            return self.get_series()[idSerie]
        return None

    def get_expire_timeout(self):
        return self._cache_timeout #seconds

    def save(self):
        xml = '<?xml version="1.0" encoding="UTF-8"?>'
        xml = xml + '\n\t<root>'
        for serie in self._series.values():
            xml = xml + '\n\t\t<serie id="'+str(serie['id'])+'" season="'+str(serie['season'])+'" chapter="'+str(serie['chapter'])+'" />'

        xml = xml + '\n\t<config>'
        xml = xml + '\n\t\t<cache_timeout value="'+ str(self.get_expire_timeout()) +'" />'
        xml = xml + '\n\t</config>'
        xml = xml + '\n\t</root>'

        f = open(self.get_data_file_path(), 'w')
        f.write(xml)
        f.close()
